<?php defined('SYSPATH') OR die('No direct access allowed.');

class Members_Controller extends Master_Club_Controller 
{
		
	public function index()
	{
		$this->page(1);
	}
	
	public function page($pagenum=1)
    {
		$view = new View('club/members');
	
		$clubs = new Clubs_Model();
        
        // Instantiate Pagination, passing it the total number of product rows.
        $paging = new Pagination(array
		(
			'base_url'    => 'club/members/page/',
			'uri_segment' => 4,
			'total_items' => $clubs->count_clubs(),
		));

        // Render the page links
        $view->pagination = $paging->render();

        // Display X items per page, from offset = page number
        $view->result = $clubs->browse($paging->items_per_page, $paging->sql_offset);
        
    	$auth = new Auth();
		if ($auth->logged_in('admin')) {
			$this->set_title('ผู้ดูแลระบบ - คลับสมาชิก');
		} 
		else if ($auth->logged_in()) {
			$this->set_title('สมาชิก - คลับสมาชิก');	
		}
		else {
			$this->set_title('คลับสมาชิก');	
		}
		
    	$this->template->content = $view;
		$this->template->render(TRUE);
    }
    
	public function show_members($club_id) {
		$view = new View("club/show_members");

		$clubs = new Clubs_Model();
		$result = $clubs->browse_all_members($club_id);
		
        $view->club_name = 'ข้อมูลสมาชิก';
		$members = array();
        
		foreach ($result as $row) {
			$name = $row->prefix . ' ' . $row->firstname . ' ' . $row->lastname;
			$uri = '';
			if (!empty($row->picture)) {
				$uri = url::base() . $row->picture;
			}
			$member = array (
				'id' => $row->id,
				'name' => $name,
				'picture' => $uri,
			);
			array_push($members, $member);
        }
        $view->members = $members;
        $view->render(TRUE);		
	}
    
}
